package com.nogroup.springbootproject.controller;

import com.nogroup.springbootproject.dto.CclassDto;
import com.nogroup.springbootproject.dto.ClassroomDto;
import com.nogroup.springbootproject.entity.Cclass;
import com.nogroup.springbootproject.entity.Classroom;
import com.nogroup.springbootproject.entity.Exam;
import com.nogroup.springbootproject.service.ClassroomService;
import com.nogroup.springbootproject.utils.PageUtils;
import com.nogroup.springbootproject.utils.result.DataResult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Classroom)表控制层
 *
 * @author makejava
 * @since 2024-04-07 18:45:32
 */
@RestController
@RequestMapping("classroom")
public class ClassroomController {
    /**
     * 服务对象
     */
    @Resource
    private ClassroomService classroomService;

    /**
     * 分页查询
     *
     * @param classroom 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Classroom>> queryByPage(Classroom classroom, PageRequest pageRequest) {
        return ResponseEntity.ok(this.classroomService.queryByPage(classroom, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Classroom> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.classroomService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param classroom 实体
     * @return 新增结果
     */
    @RequestMapping("insertClassroom")
    @PostMapping
    public DataResult insertClassroom(Classroom classroom) {
        return DataResult.successByData(classroomService.insert(classroom));
    }

    /**
     * 编辑数据
     *
     * @param classroom 实体
     * @return 编辑结果
     */
    @RequestMapping("edit")
    @PutMapping
    public DataResult edit(Classroom classroom) {
        return DataResult.successByData(this.classroomService.update(classroom));
    }

    /**
     * 删除数据
     *
     * @param classroom 主键
     * @return 删除是否成功
     */
    @RequestMapping("deleteById")
    @DeleteMapping
    public DataResult deleteById(Classroom classroom) {
        return DataResult.successByData(this.classroomService.deleteById(classroom));
    }

    @RequestMapping("getAllClassrooms")
    public DataResult getAllClassroom(){
        List<Classroom> classrooms = classroomService.getAllClassrooms();
        return DataResult.successByDatas(classrooms);
    }

    @RequestMapping("getClassroomsByFuzzySearch")
    public DataResult getClassroomsByFuzzySearch(ClassroomDto classroomDto){
        long count = classroomService.countPage(classroomDto);
        int index = PageUtils.getStartPage(classroomDto.getPage(), classroomDto.getLimit());
        classroomDto.setPage(index < count ? index : 0);
        List<Classroom> classrooms = classroomService.getClassroomsByFuzzySearch(classroomDto);
        return DataResult.successByTotalData(classrooms,count);
    }

}

